package com.jg.visitor.common.utils;

import com.jg.visitor.config.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

import java.util.Base64;

@Slf4j
public class Base64Utils {
    private static final Base64.Encoder encoder = Base64.getMimeEncoder();
    private static final Base64.Decoder decoder = Base64.getMimeDecoder();


    public static String encode(String encodeStr){
        return encoder.encodeToString(encodeStr.getBytes()).replaceAll("[\r\n]", "");
    }

    public static String decode(String decodeStr){
        try{
            return new String(decoder.decode(decodeStr));
        }catch (Exception e){
            log.error("decode error",e);
            throw new BaseException(-1,"解密token失败");
        }
    }
}